import instance from "../../utils/util"
Page({
    data: {
        indicatorDots: true,
        vertical: false,
        autoplay: false,
        interval: 2000,
        duration: 500,
        banner: [],
        cate: [],
        new: [],
        hdone: "",
        hdtwo: "",
        hdthree: "",
        like: [],
        collect: [],
        rqtj: [],
        shop: {},
    },
    onLoad(options) {
        this.getdpinfo()
        this.getbanner()
        this.getcategiry()
        this.gethd()
        // this.getcollect()


    },
    async getcollect() {

    },
    info() {
        wx.navigateTo({
            url: '/pages/dpinfo/dpinfo?id=' + this.data.shop.id,
        })
    },
    async getdpinfo() {
        const res = await instance.get("/api/shops/1");
        console.log(res)
        this.setData({
            shop: res.data
        })
    },
    boda() {
        wx.makePhoneCall({
            phoneNumber: this.data.shop.phone //仅为示例，并非真实的电话号码
        })
    },
    async getbanner() {
        const res = await instance.get("/api/config?page=1&&limit=3");
        console.log(res)
        this.setData({
            banner: res.data.list
        })
    },
    stal() {
        wx.navigateTo({
            url: '/pages/stal/stal',
        })
    },
    async getcategiry() {
        const res = await instance.get("/api/products?page=1&&limit=6&&isHot=true");
        console.log(res)
        this.setData({
            cate: res.data.list
        })
    },
    async gethd() {
        const res = await instance.get("/api/products?page=1&&limit=4&&isNew=true");
        console.log(res)



        this.setData({
            new: res.data.list,
        })
        if (wx.getStorageSync('info')) {
            let data = JSON.parse(wx.getStorageSync('info'))
            const ress = await instance.get("/api/user-favorites?userId=" + data.id);
            console.log(ress)

            console.log(this.data.new, "newss")
            let data2 = this.data.new;
            data2.forEach(el => {
                el.collect = false
                ress.data.forEach(ell => {
                    if (el.id == ell.productId) {
                        el.collect = true
                    }
                })
            })
            console.log(data2)
            this.setData({
                new: data2,
                collect: ress.data
            })
        }


    },
    async collect(e) {
        console.log(e)
        let data = JSON.parse(wx.getStorageSync('info'))
        const res = await instance.post("/api/user-favorites", {
            userId: data.id,
            productId: e.currentTarget.dataset.id
        })
        this.gethd()
    },
    async collect1(e) {
        console.log(e)
        let data = JSON.parse(wx.getStorageSync('info'))
        let id=""
        this.data.collect.forEach(el=>{
            if(el.productId == e.currentTarget.dataset.id){
id=el.id
            }
        })
        const res = await instance.delete("/api/user-favorites/" + id+"?userId="+data.id,{
            userId:data.id
        });
        this.gethd()
    },
    search() {
        console.log("123")
        wx.navigateTo({
            url: '/pages/search/search',
        })
    },

    async getlike() {
        const res = await instance.get("/index/findListGoods");
        console.log(res)
        this.setData({
            like: res.data
        })

    },
    async gettj() {
        const res = await instance.get("/index/findRecommendGoods");
        console.log(res)
        this.setData({
            rqtj: res.data
        })
    },
    tzxq(e) {
        console.log(e)
        wx.navigateTo({
            url: `/pages/shopinfo/shopinfo?id=${e.currentTarget.dataset.id}`,
        })
    },
    collectto(){
        wx.navigateTo({
          url: '/pages/collect/collect',
        })
    },
    loglist(e) {
        console.log(e)
        wx.navigateTo({
            url: `/pages/shoplist/shoplist?id=${e.currentTarget.dataset.id}`,
        })
    }

})